package com.example.weather_plus.ui.weathergram.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather_plus.R;
import com.example.weather_plus.ui.weathergram.Items.Card;
import com.example.weather_plus.ui.weathergram.Items.EntriesCardFragment;

import java.util.ArrayList;

public class EntriesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_SECTOR = -1;      // 分节符
    private static final int TYPE_ENTRIES = 0;      // 普通entries
    private final FragmentActivity activity;
    ArrayList<Card> list;
    Context context;

    public EntriesAdapter(ArrayList<Card> list, Context context, AppCompatActivity activity) {
        this.list = list;
        this.context = context;
        this.activity = activity;
    }

    /*
        根据Card类中的card_id属性设置viewType

        读取card类的id属性，viewType=
            - -1 分节视图
            -  0 普通视图
     */
    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getCardId() == -1) {
            return TYPE_SECTOR;
        } else {
            return TYPE_ENTRIES;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        /*
            根据viewType分类
         */
        if (viewType == TYPE_SECTOR) {
            View headerView = LayoutInflater.from(context).inflate(R.layout.section_break, parent, false);
            return new HeaderViewHolder(headerView);
        } else if (viewType == TYPE_ENTRIES) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.entries_item, parent, false);
            return new ItemViewHolder(itemView);
        }
        throw new RuntimeException("没有匹配的ViewType: " + viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof HeaderViewHolder) {
            // 绑定分节符数据
            ((HeaderViewHolder) holder).date.setText(list.get(position).getTitle());


        } else if (holder instanceof ItemViewHolder) {
            // 绑定普通数据项的数据

            ((ItemViewHolder) holder).date.setText(list.get(position).getDate());
            ((ItemViewHolder) holder).week.setText(list.get(position).getWeek());
            ((ItemViewHolder) holder).time.setText(list.get(position).getTime());
            ((ItemViewHolder) holder).title.setText(list.get(position).getTitle());

            // 获取当前点击卡片的id
            Card now_play = list.get(position);


            // 为ViewHolder的itemView设置点击事件监听
            holder.itemView.setOnClickListener(v -> {
                // 设置itemView（）处理点击事件
                // 点击事件：创建一个EntriesCardFragment（即日记entries弹窗），并将其添加到R.id.main（diary_activity）
                // 即点开日记本后显示的主界面
                EntriesCardFragment myFragment = new EntriesCardFragment(now_play);

                FragmentManager fragmentManager = activity.getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.main, myFragment).commit();

            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    // 用以绑定普通entries视图
    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView date;
        TextView week;
        TextView time;
        TextView title;


        public ItemViewHolder(@NonNull View itemView) {

            super(itemView);

            date = itemView.findViewById(R.id.dateTextView);
            week = itemView.findViewById(R.id.weekTextView);
            time = itemView.findViewById(R.id.timeTextView);
            title = itemView.findViewById(R.id.titleTextView);
        }
    }

    // 用以绑定分节符视图
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView date;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);

            date = itemView.findViewById(R.id.section_break);
        }

    }

}
